/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 *
 */
public class BarangGudang {

    private String idBarang;
    private String namaBarang;
    private double jumlahBarang;
    private double hargaBarang;
    String jdbcUrl = "jdbc:oracle:thin:@172.23.9.185:1521:orcl";
  String userid = "mhs115314034"; // gantikan dengan username anda
   String password = "mhs115314034"; // gantikan dengan password anda
  

    /**
     * @return the idBarang
     */
    public String getIdBarang() {
        return idBarang;
    }

    /**
     * @param idBarang the idBarang to set
     */
    public void setIdBarang(String idBarang) {
        this.idBarang = idBarang;
    }

    /**
     * @return the namaBarang
     */
    public String getNamaBarang() {
        return namaBarang;
    }

    /**
     * @param namaBarang the namaBarang to set
     */
    public void setNamaBarang(String namaBarang) {
        this.namaBarang = namaBarang;
    }

    /**
     * @return the jumlahBarang
     */
    public double getJumlahBarang() {
        return jumlahBarang;
    }

    /**
     * @param jumlahBarang the jumlahBarang to set
     */
    public void setJumlahBarang(double jumlahBarang) {
        this.jumlahBarang = jumlahBarang;
    }

    /**
     * @return the hargaBarang
     */
    public double getHargaBarang() {
        return hargaBarang;
    }

    /**
     * @param hargaBarang the hargaBarang to set
     */
    public void setHargaBarang(double hargaBarang) {
        this.hargaBarang = hargaBarang;
    }

    public void tambahBarang(BarangGudang barangGudang) {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select jumlahbarang from baranggudang where idbarang = '" + getIdBarang() + "'");
            double jumlahBarangSementara = 0;
            while (resultSet.next()) {
                jumlahBarangSementara = resultSet.getDouble("jumlahbarang");
            }
            double totalJumlahBarang = jumlahBarangSementara + getJumlahBarang();
            statement.executeUpdate("update baranggudang set jumlahbarang = " + totalJumlahBarang + " where idbarang = '" + getIdBarang() + "'");
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
        }
    }

    public void kurangBarang(BarangGudang barangGudang) {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select jumlahbarang from baranggudang where idbarang = '" + getIdBarang() + "'");
            double jumlahBarangSementara = 0;
            while (resultSet.next()) {
                jumlahBarangSementara = resultSet.getDouble("jumlahbarang");
            }
            double totalJumlahBarang = jumlahBarangSementara - getJumlahBarang();
            statement.executeUpdate("update baranggudang set jumlahbarang = " + totalJumlahBarang + " where idbarang = '" + getIdBarang() + "'");
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
        }
    }

    public void updateHargaBarangGudang(BarangGudang barangGudang) {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            statement.executeUpdate("update baranggudang set hargabarang = "
                    + getHargaBarang() + " where idbarang = '"
                    + getIdBarang() + "'");
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
        }
    }

    public ResultSet tampilBarang() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from baranggudang");
            String hasil[] = null;
            int counter = 0;
            return resultSet;
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            return null;
        }
    }
}
